#include <stdio.h>
#include <stack>
#include <stack>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <math.h>

#include <stdlib.h>
#include <string.h>
//https://mp.weixin.qq.com/s/48jMBVVEqQp3IR6NpUy2QA

using namespace cv;
using namespace std;

string int2class[20] = {"person", "aeroplane", "tv", "train", "boat", "dog", "chair", "bird", "bick", "bottle",
                     "sheep", "table", "horse", "motor", "sofa", "cow", "car", "cat", "bus", "plant"};

/*
  在图上画框
*/
void DrawRectangle(Mat &img, int* rect, string conf, int class_th, Scalar color)
{

    rectangle(img, Point(rect[0], rect[1]), Point(rect[2], rect[3]), color, 2, 8, 0);//左上角点的(x,y)，右下角点的(x,y)

    int x = rect[0] + 2;
    int y = rect[1] + 12;
    x = x > img.cols ? img.cols : x;
    y = y > img.rows ? img.rows : y;
    putText(img, conf + " " + int2class[class_th], Point(x,y), FONT_HERSHEY_SIMPLEX, 0.5, color, 1, 8);
}



/*
  将字符串按照 . 和 / 个字符分开
*/
vector<string> Split_by_point_and_slash(string strTime)
{
    vector<string> result;
    string temp("");
    strTime += '/';//字符串结束标记，方便将最后一个单词入vector
    for(size_t i = 0; i < strTime.size(); i++)
    {
        if(strTime[i] == '/' || strTime[i] == '.')
        {
            result.push_back(temp);
            temp = "";
        }
        else
        {
            temp += strTime[i];
        }
    }
    return result;
}




//#define save_video

/*
  main
*/
int main(int arg, char* argv[])
{
    // "/home/ccc/桌面/get_plane_img/vgg_3w_plane.txt"
    // "/home/ccc/桌面/get_plane_img/mobile_3w_plane.txt"
    ifstream fstr_test("/home/ccc/030/Hi3559AV100R001C02SPC030/01.software/board/Hi3559AV100_SDK_V2.0.3.0/mpp/sample/svp/multi-core/nnie/retinanet/bbox_result.txt"); // 测试结果
    string img_pos = "/home/ccc/030/Hi3559AV100R001C02SPC030/01.software/board/Hi3559AV100_SDK_V2.0.3.0/mpp/sample/svp/multi-core/nnie/retinanet/img_jpg/"; // 图片位置
    string img_save_pos = "./result/"; // 图片保存位置
    int img_width = 608, img_height = 608; // 检测图像的宽/高

#ifdef save_video
    VideoWriter video("F22.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(1280, 720));
#endif

    string img_name_jpg; // 图片名字
    string img_route; // 图像路径
    Mat src_img;
    float factor_expand[2]; // 检测结果在映射到图片上

    string test_string, test_tmp_str; // 测试文件的数据流 及缓存
    string gt_string, gt_tmp_str; // ground truth的数据流
    int count_pic_num = 0;
    int color_detection = 255;
    while (getline(fstr_test, test_string))
    {
        string conf;
        int class_th = 0;
        int rect_pos[4] = {0};
        cout << endl << count_pic_num << "th img:" << endl;

        istringstream test_str_stream(test_string); // 将list.txt中的每一行作为一个数据流
        int test_str_ith = 0; // string中，按照空格划分，第i个字符串
//        cout << endl;
        while(test_str_stream >> test_tmp_str) // 按照空格拆分string
        {
//            cout << test_tmp_str << endl;
            if(test_str_ith == 0)
            {
                vector<string> split_str;
                split_str = Split_by_point_and_slash(test_tmp_str);
                img_name_jpg = split_str[4] + ".jpg"; // ******这里需要设置img name是第几个
                img_route = img_pos + img_name_jpg;
                cout << "img name: " << img_name_jpg << endl;

                src_img = imread(img_route); // 读取图片
                // 打印分割后的字符串
//                cout << endl;
//                for(int ii=0; ii<split_str.size(); ii++)
//                    cout << ii << ": " << split_str[ii] << endl;

                factor_expand[0] = src_img.cols * 1.0f / img_width;
                factor_expand[1] = src_img.rows * 1.0f / img_height;

//                color_detection = 0;
            }
            else if(test_str_ith % 6 == 1) conf = test_tmp_str.substr(0, 4).c_str(); // conf
            else if(test_str_ith % 6 == 2) class_th = atoi(test_tmp_str.c_str()); // class
            else if(test_str_ith % 6 == 3) rect_pos[0] = atoi(test_tmp_str.c_str()) * factor_expand[0]; // x1
            else if(test_str_ith % 6 == 4) rect_pos[1] = atoi(test_tmp_str.c_str()) * factor_expand[1]; // y1
            else if(test_str_ith % 6 == 5) rect_pos[2] = atoi(test_tmp_str.c_str()) * factor_expand[0]; // x2
            else if(test_str_ith % 6 == 0) // y2
            {
                rect_pos[3] = atoi(test_tmp_str.c_str()) * factor_expand[1];
                cout << int2class[class_th] << " " << conf << " " << rect_pos[0] << " " << rect_pos[1] << " " << rect_pos[2] << " " << rect_pos[3] << endl;
                DrawRectangle(src_img, rect_pos, conf, class_th, Scalar(0, 0, 255)); // 蓝 test
                color_detection += 20;
                if(color_detection > 255) color_detection = 255;
            }

            test_str_ith ++;
        }
#ifdef save_video
        resize(src_img, src_img, Size(1280, 720));
        video << src_img;
#endif
        imshow("original", src_img);
        if(waitKey(0) == 1048603) break;

        string save_route = img_save_pos + img_name_jpg;
//        imwrite(save_route, src_img); // 保存图片

        count_pic_num++;

//        break;
    }




    return 0;
}


